//
//  CustomeButton.m
//  YJMiGao
//
//  Created by 张龙 on 16/12/5.
//  Copyright © 2016年 张龙. All rights reserved.
//
#import "CustomeButton.h"

@interface CustomeButton()

@property(nonatomic,assign)CGRect imageFrame;
@property(nonatomic,assign)CGRect titleFrame;


@end

@implementation CustomeButton
- (id)initWithRelativeAlignment:(KRelativeAlignment )relative titleNormal:(NSString *)titleNormal imageNormal:(NSString*)imageNormal frame:(CGRect)frame titleRect:(CGRect )titleRect imageRect:(CGRect)imageRect{
    if (!self) {
        self=[super init];
    }
    self.relative = relative;
    self.frame = frame;
    [self setTitle:titleNormal forState:UIControlStateNormal];
    [self setImage:[UIImage imageNamed:imageNormal] forState:UIControlStateNormal];
    self.titleRect = titleRect;
    self.imageRect = imageRect;
    return self;
}
- (id)initWithRelativeAlignment:(KRelativeAlignment )relative titleNormal:(NSString *)titleNormal imageNormal:(NSString*)imageNormal frame:(CGRect)frame titleRect:(CGRect )titleRect imageRect:(CGRect)imageRect textAlignment:(NSTextAlignment)textAlignment font:(UIFont *)font titleColor:(UIColor*)titleColor{
    
    if (self = [super init]) {
        self = [self initWithRelativeAlignment:relative titleNormal:titleNormal imageNormal:imageNormal frame:frame titleRect:titleRect imageRect:imageRect];
        self.titleLabel.textAlignment = textAlignment;
        self.titleLabel.font = font;
//        self.titleLabel.backgroundColor = [UIColor redColor];
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self setTitleColor:titleColor forState:UIControlStateNormal];
    }
    return self;
}

- (id)initWithImageFrame:(CGRect)imageF andTitleFrame:(CGRect)titleF{
    self = [super init];
    if (self) {
        self.imageFrame = imageF;
        self.titleFrame = titleF;
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    double width;
    if (strIsEmpty(self.fontText)) {
        width = [Global getSizeOfString:self.titleLabel.text maxWidth:kScreenW maxHeight:20 withFontSize:16].width;
    }else{
        width = [Global getSizeOfString:self.titleLabel.text maxWidth:kScreenW maxHeight:20 withFontSize:[self.fontText intValue]].width;
    }
    
    if (CGRectEqualToRect(self.imageRect, CGRectZero)) {
        switch (self.relative) {
            case KRelativeAlignmentDown:
                self.imageView.frame=CGRectMake(self.frame.size.width / 3, self.frame.size.height / 4, self.frame.size.width / 3, self.frame.size.width / 3);
                break;
            case KRelativeAlignmentLeft:
                self.imageView.frame=CGRectMake(width + 3, 0, 20, 22);
                break;
            case KRelativeAlignmentRight:
                self.imageView.frame=CGRectMake(0, 0, 13, 15);
                break;
            default:
                break;
        }
    }else{
        self.imageView.frame = self.imageRect;
    }
    if (CGRectEqualToRect(self.titleRect, CGRectZero)) {
        switch (self.relative) {
            case KRelativeAlignmentDown:
                self.titleLabel.frame=CGRectMake(0, CGRectGetMaxY(self.imageView.frame) + 10, CGRectGetWidth(self.frame), 20);
                break;
            case KRelativeAlignmentLeft:
                self.titleLabel.frame=CGRectMake(0, 0, width, 20);
                break;
            case KRelativeAlignmentRight:
                self.titleLabel.frame=CGRectMake(CGRectGetMaxX(self.imageView.frame) + 5, 0, width, 20);
                break;
            default:
                break;
        }
    }else{
        self.titleLabel.frame = self.titleRect;
    }
    
    
//    if (self.relative == KRelativeAlignmentDown) {
//
//        if (CGRectEqualToRect(self.imageRect, CGRectZero)) {
//            self.imageView.frame=CGRectMake(self.frame.size.width / 3, self.frame.size.height / 4, self.frame.size.width / 3, self.frame.size.width / 3);
//        }else{
//            self.imageView.frame=CGRectMake(self.imageRect.origin.x, self.imageRect.origin.y,self.imageRect.size.width, self.imageRect.size.height);
//        }
//        if (CGRectEqualToRect(self.titleRect, CGRectZero)) {
//            self.titleLabel.frame=CGRectMake(0, CGRectGetMaxY(self.imageView.frame) + 10, CGRectGetWidth(self.frame), 20);
//        }else{
//            self.titleLabel.frame=CGRectMake(self.titleRect.origin.x, self.titleRect.origin.y, self.titleRect.size.width, self.titleRect.size.height);
//        }
//    }else if (self.relative == KRelativeAlignmentLeft) {
//        if (CGRectEqualToRect(self.titleRect, CGRectZero)) {
//            self.titleLabel.frame=CGRectMake(23, 0, width, 20);
//
//        }else{
//            self.titleLabel.frame=CGRectMake(self.titleRect.origin.x, self.titleRect.origin.y, self.titleRect.size.width, self.titleRect.size.height);
//        }
//        if (CGRectEqualToRect(self.imageRect, CGRectZero)) {
//            self.imageView.frame=CGRectMake(width + 3, 0, 20, 22);
//        }else{
//            self.imageView.frame=CGRectMake(self.imageRect.origin.x, self.imageRect.origin.y,self.imageRect.size.width, self.imageRect.size.height);
//        }
//
//    }else if (self.relative == KRelativeAlignmentRight) {
//        if (CGRectEqualToRect(self.imageRect, CGRectZero)) {
//            self.imageView.frame=CGRectMake(0, 0, 13, 15);
//        }else{
//            self.imageView.frame=CGRectMake( self.imageRect.origin.x, self.imageRect.origin.y,self.imageRect.size.width, self.imageRect.size.height);
//        }
//        if (CGRectEqualToRect(self.titleRect, CGRectZero)) {
//            self.titleLabel.frame=CGRectMake(23, 0, width, 20);
//        }else{
//            self.titleLabel.frame=CGRectMake(self.titleRect.origin.x, self.titleRect.origin.y, self.titleRect.size.width, self.titleRect.size.height);
//        }
//
//    }

}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
